package com.sktk.keepAccount.common.web.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm";
    public static String DATE_FORMAT = "yyyy-MM-dd";
    public static String MONTH_FORMAT = "yyyy-MM";
    /** 日期 */
    public final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    /** 日期时间 */
    public final static String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /** 时间 */
    public final static String DEFAULT_TIME_PATTERN = "HH:mm:ss";

    /**
     * Get the previous time, from how many days to now.
     *
     * @param days
     *            How many days.
     * @return The new previous time.
     */
    public static Date previous(int days) {
        return new Date(System.currentTimeMillis() - days * 3600000L * 24L);
    }

    /**
     * Convert date and time to string like "yyyy-MM-dd HH:mm".
     */
    public static String formatDateTime(Date d) {
        return new SimpleDateFormat(DATETIME_FORMAT).format(d);
    }

    /**
     * Convert date and time to string like "yyyy-MM-dd HH:mm".
     */
    public static String formatDateTime(long d) {
        return new SimpleDateFormat(DATETIME_FORMAT).format(d);
    }

    /**
     * Parse date like "yyyy-MM-dd".
     */
    public static Date parseDate(String d) {
        try {
            return new SimpleDateFormat(DATE_FORMAT).parse(d);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return null;
    }

    /**
     * Parse date and time like "yyyy-MM-dd hh:mm".
     */
    public static Date parseDateTime(String dt) {
        try {
            return new SimpleDateFormat(DATETIME_FORMAT).parse(dt);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return null;
    }

    /**
     * 转换日期字符串得到指定格式的日期类型
     *
     * @param formatString
     *            需要转换的格式字符串
     * @param targetDate
     *            需要转换的时间
     * @return
     * @throws ParseException
     */
    public static final Date convertString2Date(String formatString,
                                                String targetDate) throws ParseException {
        if (StringUtils.isBlank(targetDate))
            return null;
        SimpleDateFormat format = null;
        Date result = null;
        format = new SimpleDateFormat(formatString);
        try {
            result = format.parse(targetDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return result;
    }

    public static final Date convertString2Date(String[] formatString,
                                                String targetDate) throws ParseException {
        if (StringUtils.isBlank(targetDate)) {
            return null;
        }
        SimpleDateFormat format = null;
        Date result = null;
        String errorMessage = null;
        Integer errorOffset = null;
        for (String dateFormat : formatString) {
            try {
                format = new SimpleDateFormat(dateFormat);
                result = format.parse(targetDate);
            } catch (ParseException pe) {
                result = null;
                errorMessage = pe.getMessage();
                errorOffset = pe.getErrorOffset();
            } finally {
                if (result != null && result.getTime() > 1) {
                    break;
                }
            }
        }
        if (result == null) {
            throw new ParseException(errorMessage, errorOffset);
        }
        return result;
    }

    /**
     * 转换字符串得到默认格式的日期类型
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static Date convertString2Date(String strDate) throws ParseException {
        Date result = null;
        try {
            result = convertString2Date(DEFAULT_DATE_PATTERN, strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return result;
    }

    /**
     * 转换字符串得到默认格式的日期类型
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static Date convertString2Datetime(String strDate) throws ParseException {
        Date result = null;
        try {
            result = convertString2Date(DEFAULT_DATE_TIME_PATTERN, strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return result;
    }

    /**
     * 转换日期得到指定格式的日期字符串
     *
     * @param formatString
     *            需要把目标日期格式化什么样子的格式。例如,yyyy-MM-dd HH:mm:ss
     * @param targetDate
     *            目标日期
     * @return
     */
    public static String convertDate2String(String formatString, Date targetDate) {
        SimpleDateFormat format = null;
        String result = null;
        if (targetDate != null) {
            format = new SimpleDateFormat(formatString);
            result = format.format(targetDate);
        } else {
            return null;
        }
        return result;
    }

    /**
     * 转换日期,得到默认日期格式字符串
     *
     * @param targetDate
     * @return
     */
    public static String convertDate2String(Date targetDate) {
        return convertDate2String(DEFAULT_DATE_PATTERN, targetDate);
    }

    /**
     * 转换日期,得到默认日期格式字符串
     *
     * @param targetDate
     * @return
     */
    public static String convertDatetime2String(Date targetDate) {
        return convertDate2String(DEFAULT_DATE_TIME_PATTERN, targetDate);
    }

    /**
     * 比较日期大小
     *
     * @param src
     * @param src
     * @return int; 1:DATE1>DATE2;
     */
    public static int compareDate(Date src, Date src1) {

        String date1 = convertDate2String(DEFAULT_DATE_TIME_PATTERN, src);
        String date2 = convertDate2String(DEFAULT_DATE_TIME_PATTERN, src1);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return 0;
    }

    /**
     * 日期比较
     *
     * 判断时间date1是否在时间date2之前 <br/>
     * 时间格式 2005-4-21 16:16:34 <br/>
     * 添加人：胡建国
     *
     * @param
     * @return
     */
    public static boolean isDateBefore(String date1, String date2) {
        try {
            DateFormat df = DateFormat.getDateTimeInstance();
            return df.parse(date1).before(df.parse(date2));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 日期比较
     *
     * 判断当前时间是否在时间date2之前 <br/>
     * 时间格式 2005-4-21 16:16:34 <br/>
     * 添加人：胡建国
     *
     * @param
     * @return
     */
    public static boolean isDateBefore(String date2) {
        if (date2 == null) {
            return false;
        }
        try {
            Date date1 = new Date();
            DateFormat df = DateFormat.getDateTimeInstance();
            return date1.before(df.parse(date2));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 比较当前时间与时间date2的天相等 时间格式 2008-11-25 16:30:10 如:当前时间是2008-11-25
     * 16:30:10与传入时间2008-11-25 15:31:20 相比较,返回true即相等
     *
     * @param
     * @param date2
     * @return boolean; true:相等
     * @author zhangjl
     */
    public static boolean equalDate(String date2) {
        try {
            String date1 = convertDate2String(DEFAULT_DATE_TIME_PATTERN,
                    new Date());
            date1.equals(date2);
            Date d1 = convertString2Date(DEFAULT_DATE_PATTERN, date1);
            Date d2 = convertString2Date(DEFAULT_DATE_PATTERN, date2);
            return d1.equals(d2);
        } catch (ParseException e) {
            logger.error(e.getLocalizedMessage());
            return false;
        }
    }

    /**
     * 比较时间date1与时间date2的天相等 时间格式 2008-11-25 16:30:10
     *
     * @param date1
     * @param date2
     * @return boolean; true:相等
     * @author zhangjl
     */
    public static boolean equalDate(String date1, String date2) {
        try {

            Date d1 = convertString2Date(DEFAULT_DATE_PATTERN, date1);
            Date d2 = convertString2Date(DEFAULT_DATE_PATTERN, date2);

            return d1.equals(d2);
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 比较时间date1是否在时间date2之前 时间格式 2008-11-25 16:30:10
     *
     * @param date1
     * @param date2
     * @return boolean; true:在date2之前
     * @author 胡建国
     */
    public static boolean beforeDate(String date1, String date2) {
        try {
            Date d1 = convertString2Date(DEFAULT_DATE_PATTERN, date1);
            Date d2 = convertString2Date(DEFAULT_DATE_PATTERN, date2);
            return d1.before(d2);
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 获取两个时间的时间间隔
     *
     * @param beginDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     */
    public static int getSecondsBetween(Date beginDate, Date endDate) {

        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        int betweenSeconds = (int)((endTime - beginTime) / 1000);//先算出两时间的毫秒数之差

        return betweenSeconds;

    }

    /**
     * 获取两个时间的时间间隔
     *
     * @param beginDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     */
    public static int getDaysBetween(Date beginDate, Date endDate) {

        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        int betweenDays = (int)((endTime - beginTime) / (1000 * 60 * 60 *24));//先算出两时间的毫秒数之差大于一天的天数

        endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);//使endCalendar减去这些天数，将问题转换为两时间的毫秒数之差不足一天的情况
        endCalendar.add(Calendar.DAY_OF_MONTH, -1);//再使endCalendar减去1天
        if(beginCalendar.get(Calendar.DAY_OF_MONTH)==endCalendar.get(Calendar.DAY_OF_MONTH))//比较两日期的DAY_OF_MONTH是否相等
            return betweenDays + 1;	//相等说明确实跨天了
        else
            return betweenDays + 0;	//不相等说明确实未跨天

    }

    /**
     * 获取两个时间的时间间隔(月份)
     *
     * @param beginDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @return
     */
    public static int getMonthsBetween(Date beginDate, Date endDate) {
        if (beginDate.after(endDate)) {
            Date swap = beginDate;
            beginDate = endDate;
            endDate = swap;
        }
        int months = endDate.getMonth() - beginDate.getMonth();
        int years = endDate.getYear() - beginDate.getYear();

        months += years * 12;

        return months;
    }

    /**
     * 根据当前给定的日期获取当前天是星期几(中国版的)
     *
     * @param date
     *            任意时间
     * @return
     */
    public static String getChineseWeek(Calendar date) {
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六" };
        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
        return dayNames[dayOfWeek - 1];

    }

    /**
     * 获得日期的下一个星期一的日期
     *
     * @param date
     *            任意时间
     * @return
     */
    public static Calendar getNextMonday(Calendar date) {
        Calendar result = null;
        result = date;
        do {
            result = (Calendar) result.clone();
            result.add(Calendar.DATE, 1);
        } while (result.get(Calendar.DAY_OF_WEEK) != 2);
        return result;
    }


    public static boolean isDateEnable(Date beginDate, Date endDate,
                                       Date currentDate) {
        // 开始日期
        long beginDateLong = beginDate.getTime();
        // 结束日期
        long endDateLong = endDate.getTime();
        // 当前日期
        long currentDateLong = currentDate.getTime();
        if (currentDateLong >= beginDateLong && currentDateLong <= endDateLong) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 加减天数
     *
     * @param num
     * @param Date
     * @return
     */
    public static Date addDay(int num, Date Date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date);
        calendar.add(Calendar.DATE, num);// 把日期往后增加 num 天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }

    /**
     * 加减月数
     *
     * @param num
     * @param Date
     * @return
     */
    public static Date addMonth(int num, Date Date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date);
        calendar.add(Calendar.MONTH, num);// 把日期往后增加 num 天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }

    /**
     * 加减天数
     *
     * @param num
     * @param Date
     * @return
     */
    public static Date addSeconds(int num, Date Date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date);
        calendar.add(Calendar.SECOND, num);// 把日期往后增加 num 天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }

    /**
     * 计算两端时间的小时差
     *
     * @param begin
     * @param end
     * @return
     */
    public static int getHour(Date begin, Date end) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(begin);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(end);
        Long millisecond = c2.getTimeInMillis() - c1.getTimeInMillis();
        Long hour = millisecond / 1000 / 60 / 60;
        Long minute = (millisecond / 1000 / 60) % 60;
        if (minute >= 30) {
            hour++;
        }

        return hour.intValue();
    }

    /**
     * 取得指定时间的给定格式
     * @return String
     * @throws ParseException
     */
    public static String setDateFormat(Date myDate, String strFormat)
            throws ParseException {
        if (myDate == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        String sDate = sdf.format(myDate);
        return sDate;
    }

    public static String setDateFormat(String myDate, String strFormat)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        String sDate = sdf.format(myDate);

        return sDate;
    }

    /*****************************************
     * @功能 计算某年某月的结束日期
     * @return interger
     * @throws ParseException
     ****************************************/
    public static String getYearMonthEndDay(int yearNum, int monthNum)
            throws ParseException {

        // 分别取得当前日期的年、月、日
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(monthNum);
        String tempDay = "31";
        if (tempMonth.equals("1") || tempMonth.equals("3")
                || tempMonth.equals("5") || tempMonth.equals("7")
                || tempMonth.equals("8") || tempMonth.equals("10")
                || tempMonth.equals("12")) {
            tempDay = "31";
        }
        if (tempMonth.equals("4") || tempMonth.equals("6")
                || tempMonth.equals("9") || tempMonth.equals("11")) {
            tempDay = "30";
        }
        if (tempMonth.equals("2")) {
            if (isLeapYear(yearNum)) {
                tempDay = "29";
            } else {
                tempDay = "28";
            }
        }
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return tempDate;// setDateFormat(tempDate,"yyyy-MM-dd");
    }

    /*****************************************
     * @功能 判断某年是否为闰年
     * @return boolean
     * @throws ParseException
     ****************************************/
    public static boolean isLeapYear(int yearNum) {
        boolean isLeep = false;
        /** 判断是否为闰年，赋值给一标识符flag */
        if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
            isLeep = true;
        } else if (yearNum % 400 == 0) {
            isLeep = true;
        } else {
            isLeep = false;
        }
        return isLeep;
    }

    /**
     * 格式化日期
     *
     * @throws ParseException
     *
     *             例: DateUtils.formatDate("yyyy-MM-dd HH",new Date())
     *             "yyyy-MM-dd HH:00:00"
     */
    public static Date formatDate(String formatString, Date date)
            throws ParseException {
        if (date == null) {
            date = new Date();
        }
        if (StringUtils.isBlank(formatString))
            formatString = DateUtils.DEFAULT_DATE_PATTERN;

        date = DateUtils.convertString2Date(formatString, DateUtils
                .convertDate2String(formatString, date));

        return date;
    }

    /**
     * 格式化日期 yyyy-MM-dd
     *
     * @throws ParseException
     *             例： DateUtils.formatDate(new Date()) "yyyy-MM-dd 00:00:00"
     */
    public static Date formatDate(Date date) throws ParseException {
        date = formatDate(DateUtils.DEFAULT_DATE_PATTERN, date);
        return date;
    }

    /**
     * @throws ParseException
     *             根据日期获得 星期一的日期
     *
     */
    public static Date getMonDay(Date date) throws ParseException {

        Calendar cal = Calendar.getInstance();
        if (date == null)
            date = new Date();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            cal.add(Calendar.WEEK_OF_YEAR, -1);

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        date = formatDate(cal.getTime());

        return date;
    }

    /**
     * @throws ParseException
     *             根据日期获得 星期日 的日期
     *
     */
    public static Date getSunDay(Date date) throws ParseException {

        Calendar cal = Calendar.getInstance();
        if (date == null)
            date = new Date();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
            cal.add(Calendar.WEEK_OF_YEAR, 1);

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        date = formatDate(cal.getTime());
        return date;
    }

    /**
     * 获得 下个月的第一天
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getNextDay(Date date) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
        return formatDate(cal.getTime());
    }

    /**
     *
     * @return
     */
    public static String getCurrentMonth() {
        SimpleDateFormat df = new SimpleDateFormat(MONTH_FORMAT);//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static Integer getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static Integer getCurrentIntMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    public static String getAddMonthStr(String str, int addMonth) {

        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = sdf.parse(str);//初始日期
        }catch(Exception e){

        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.MONTH, addMonth);//在日历的月份上增加6个月

        SimpleDateFormat df = new SimpleDateFormat(MONTH_FORMAT);//设置日期格式
        return df.format(c.getTime());// new Date()为获取当前系统时间

    }

    /**
     *
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    /**
     * 获取当前数据第几个时间组
     * @return
     */
    public static String getTimeGroupTime(int minutesGap) {

        Date now = new Date();
        Calendar nowCalendar = Calendar.getInstance();

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式
        String str = df.format(now);// new Date()为获取当前系统时间

        Integer minute = nowCalendar.get(Calendar.MINUTE);
        Integer times = minute / minutesGap;

        Integer lastMinute = times * minutesGap;

        return str + lastMinute;

    }

    public static String getCurrentWeek() {

        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一周的第一天
        cal.setTime(date);
        int num = cal.get(Calendar.WEEK_OF_YEAR);
        SimpleDateFormat df = new SimpleDateFormat("yyyy");//设置日期格式
        return df.format(date) + "-" + num;
    }

    /**
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getAddedDayTime(String str, int addDay) {

        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = sdf.parse(str);//初始日期
        }catch(Exception e){

        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.DAY_OF_YEAR,addDay);//在日历的月份上增加天数
        return sdf.format(c.getTime());

    }

    public static String getAddedMonthTime(String str, int addMonth) {

        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = sdf.parse(str);//初始日期
        }catch(Exception e){

        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.MONTH,addMonth);//在日历的月份上增加6个月
        return sdf.format(c.getTime());

    }
    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    public static boolean isValidDateTime(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static Long getCurrentTimeStamp() {
        long now = new Date().getTime();
        return now / 1000;
    }



    public static Long getNextDayZeroTime() {
        long current = System.currentTimeMillis();// 当前时间毫秒数
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long tomorrowZero = calendar.getTimeInMillis();
        long tomorrowZeroSeconds = (tomorrowZero- current) / 1000;
        return tomorrowZeroSeconds;
    }

    /**
     * 获取昨天的时间
     */
    public static Date getYesterday() throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String format = dateFormat.format(calendar.getTime());
        return dateFormat.parse(format);
    }

    /**
     * 获取后天的最后时间
     */
    public static Long getAfterTomorrow() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 48);
        String format = dateFormat.format(calendar.getTime());
        dateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN);
        try {
           return dateFormat.parse(format+" 23:59:59").getTime();
        }catch (Exception e){
            return 0L;
        }

    }

    /**
     * 判断是否今天
     * @param day 时间
     * @return true是
     */
    public static boolean isToday(Date day)  {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(day);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断时间是否昨天
     * @param day 时间
     * @return true 是 false 否
     */
    public static boolean isYesterday(Date day) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(day);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取前天的时间
     * @return
     */
    public static String getBeforeYesterday()  {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -48);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取昨日时间
     */
    public static String getYesterdayDate() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(getYesterday());
    }

    /**
     * 获取某月有多少天
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

}
